
import { menuModel, starModel } from "./model/menuModel";
const xlsx = require('node-xlsx');
import * as fs from 'fs'
class Test {
    constructor() {

    }
    async excel() {
        let dishes = await menuModel.find({})

        let cache = {}
        let excel: any = []
        excel.push(['菜品名称', '平均评分', '被点次数'])

        dishes.map(item => {
            cache[item._id] = item
        })
        for (var key in cache) {

            if (cache.hasOwnProperty(key)) {
                let item = cache[key];
                let menuId = item._id
                let name = item.name
                let star = item.star
                let count = await starModel.count({ menuId: menuId })
                excel.push([name, star, count])
            }
        }
        let buffer = xlsx.build([{ name: `菜品详情表`, data: excel }]);
        fs.writeFileSync('./菜品详情表.xlsx', buffer);
    }
    async test() {
        let num: any[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

        let sum: any = 0;
        sum += await num.forEach(element => {
            return element;
        });
        // sum = sum + num.map( item => {
        //     return item
        // })
        console.log(sum)
    }

    async createSter() {
        for (var i = 0; i < 5; i++) {
            await starModel.create({ userId: i, star: 4 })
        }
    }

    async query() {
        // let find: any = await starModel.find({},{ star: 1 });
        let find: any = await starModel.aggregate([{
            $group: {
                _id: null,
                val: { $avg: '$star' }
            }
        }])
        console.log('find__________________', find);
    }

}

let test = new Test();
// test.createSter();
test.excel();
export default test;
